home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 151 / cd-rom 151.iso / internet / firefox / Firefox Setup 3.0 Beta 1.exe / nonlocalized / components / nsBrowserContentHandler.js < prev    next >
Encoding:
Text File  |  2007-11-09  |  30.3 KB  |  874 lines

  1. //@line 37 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  2.  
  3. const nsISupports            = Components.interfaces.nsISupports;
  4.  
  5. const nsIBrowserDOMWindow    = Components.interfaces.nsIBrowserDOMWindow;
  6. const nsIBrowserHandler      = Components.interfaces.nsIBrowserHandler;
  7. const nsIBrowserHistory      = Components.interfaces.nsIBrowserHistory;
  8. const nsIChannel             = Components.interfaces.nsIChannel;
  9. const nsICommandLine         = Components.interfaces.nsICommandLine;
  10. const nsICommandLineHandler  = Components.interfaces.nsICommandLineHandler;
  11. const nsIContentHandler      = Components.interfaces.nsIContentHandler;
  12. const nsIDocShellTreeItem    = Components.interfaces.nsIDocShellTreeItem;
  13. const nsIDOMChromeWindow     = Components.interfaces.nsIDOMChromeWindow;
  14. const nsIDOMWindow           = Components.interfaces.nsIDOMWindow;
  15. const nsIFactory             = Components.interfaces.nsIFactory;
  16. const nsIFileURL             = Components.interfaces.nsIFileURL;
  17. const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
  18. const nsIInterfaceRequestor  = Components.interfaces.nsIInterfaceRequestor;
  19. const nsINetUtil             = Components.interfaces.nsINetUtil;
  20. const nsIPrefBranch          = Components.interfaces.nsIPrefBranch;
  21. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  22. const nsISupportsString      = Components.interfaces.nsISupportsString;
  23. const nsIURIFixup            = Components.interfaces.nsIURIFixup;
  24. const nsIWebNavigation       = Components.interfaces.nsIWebNavigation;
  25. const nsIWindowMediator      = Components.interfaces.nsIWindowMediator;
  26. const nsIWindowWatcher       = Components.interfaces.nsIWindowWatcher;
  27. const nsICategoryManager     = Components.interfaces.nsICategoryManager;
  28. const nsIWebNavigationInfo   = Components.interfaces.nsIWebNavigationInfo;
  29. const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  30. const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
  31.  
  32. const NS_BINDING_ABORTED = 0x804b0002;
  33. const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
  34. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  35.  
  36. const URI_INHERITS_SECURITY_CONTEXT = nsIHttpProtocolHandler
  37.                                         .URI_INHERITS_SECURITY_CONTEXT;
  38.  
  39. function shouldLoadURI(aURI) {
  40.   if (aURI && !aURI.schemeIs("chrome"))
  41.     return true;
  42.  
  43.   dump("*** Preventing external load of chrome: URI into browser window\n");
  44.   dump("    Use -chrome <uri> instead\n");
  45.   return false;
  46. }
  47.  
  48. function resolveURIInternal(aCmdLine, aArgument) {
  49.   var uri = aCmdLine.resolveURI(aArgument);
  50.  
  51.   if (!(uri instanceof nsIFileURL)) {
  52.     return uri;
  53.   }
  54.  
  55.   try {
  56.     if (uri.file.exists())
  57.       return uri;
  58.   }
  59.   catch (e) {
  60.     Components.utils.reportError(e);
  61.   }
  62.  
  63.   // We have interpreted the argument as a relative file URI, but the file
  64.   // doesn't exist. Try URI fixup heuristics: see bug 290782.
  65.  
  66.   try {
  67.     var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  68.                              .getService(nsIURIFixup);
  69.  
  70.     uri = urifixup.createFixupURI(aArgument, 0);
  71.   }
  72.   catch (e) {
  73.     Components.utils.reportError(e);
  74.   }
  75.  
  76.   return uri;
  77. }
  78.  
  79. const OVERRIDE_NONE        = 0;
  80. const OVERRIDE_NEW_PROFILE = 1;
  81. const OVERRIDE_NEW_MSTONE  = 2;
  82. /**
  83.  * Determines whether a home page override is needed.
  84.  * Returns:
  85.  *  OVERRIDE_NEW_PROFILE if this is the first run with a new profile.
  86.  *  OVERRIDE_NEW_MSTONE if this is the first run with a build with a different
  87.  *                      Gecko milestone (i.e. right after an upgrade).
  88.  *  OVERRIDE_NONE otherwise.
  89.  */
  90. function needHomepageOverride(prefb) {
  91.   var savedmstone = null;
  92.   try {
  93.     savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
  94.   } catch (e) {}
  95.  
  96.   if (savedmstone == "ignore")
  97.     return OVERRIDE_NONE;
  98.  
  99.   var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
  100.                          .getService(nsIHttpProtocolHandler).misc;
  101.  
  102.   if (mstone != savedmstone) {
  103.     prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
  104.     return (savedmstone ? OVERRIDE_NEW_MSTONE : OVERRIDE_NEW_PROFILE);
  105.   }
  106.  
  107.   return OVERRIDE_NONE;
  108. }
  109.  
  110. // Copies a pref override file into the user's profile pref-override folder,
  111. // and then tells the pref service to reload it's default prefs.
  112. function copyPrefOverride() {
  113.   try {
  114.     var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  115.                                 .getService(Components.interfaces.nsIProperties);
  116.     const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
  117.     var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
  118.                                        Components.interfaces.nsIFile);
  119.     if (!prefOverride.exists())
  120.       return; // nothing to do
  121.  
  122.     const NS_APP_PREFS_OVERRIDE_DIR     = "PrefDOverride";
  123.     var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
  124.                                            Components.interfaces.nsIFile);
  125.  
  126.     // Check for any existing pref overrides, and remove them if present
  127.     var existingPrefOverridesFile = prefOverridesDir.clone();
  128.     existingPrefOverridesFile.append(prefOverride.leafName);
  129.     if (existingPrefOverridesFile.exists())
  130.       existingPrefOverridesFile.remove(false);
  131.  
  132.     prefOverride.copyTo(prefOverridesDir, null);
  133.  
  134.     // Now that we've installed the new-profile pref override file,
  135.     // re-read the default prefs.
  136.     var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
  137.                                .getService(Components.interfaces.nsIObserver);
  138.     prefSvcObs.observe(null, "reload-default-prefs", null);
  139.   } catch (ex) {
  140.     Components.utils.reportError(ex);
  141.   }
  142. }
  143.  
  144. function openWindow(parent, url, target, features, args) {
  145.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  146.                          .getService(nsIWindowWatcher);
  147.  
  148.   var argstring;
  149.   if (args) {
  150.     argstring = Components.classes["@mozilla.org/supports-string;1"]
  151.                             .createInstance(nsISupportsString);
  152.     argstring.data = args;
  153.   }
  154.   return wwatch.openWindow(parent, url, target, features, argstring);
  155. }
  156.  
  157. function openPreferences() {
  158.   var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
  159.   var url = "chrome://browser/content/preferences/preferences.xul";
  160.  
  161.   var win = getMostRecentWindow("Browser:Preferences");
  162.   if (win) {
  163.     win.focus();
  164.   } else {
  165.     openWindow(null, url, "_blank", features);
  166.   }
  167. }
  168.  
  169. function getMostRecentWindow(aType) {
  170.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  171.                      .getService(nsIWindowMediator);
  172.   return wm.getMostRecentWindow(aType);
  173. }
  174.  
  175. //@line 218 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  176.  
  177. // this returns the most recent non-popup browser window
  178. function getMostRecentBrowserWindow() {
  179.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  180.                      .getService(Components.interfaces.nsIWindowMediator);
  181.  
  182. //@line 238 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  183.   var windowList = wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
  184.   if (!windowList.hasMoreElements())
  185.     return null;
  186.  
  187.   var win = windowList.getNext();
  188.   while (win.document.documentElement.getAttribute("chromehidden")) {
  189.     if (!windowList.hasMoreElements()) 
  190.       return null;
  191.  
  192.     win = windowList.getNext();
  193.   }
  194. //@line 250 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  195.  
  196.   return win;
  197. }
  198.  
  199. function doSearch(searchTerm, cmdLine) {
  200.   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
  201.                      .getService(nsIBrowserSearchService);
  202.  
  203.   var submission = ss.defaultEngine.getSubmission(searchTerm, null);
  204.  
  205.   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
  206.   var sa = Components.classes["@mozilla.org/supports-array;1"]
  207.                      .createInstance(Components.interfaces.nsISupportsArray);
  208.  
  209.   var wuri = Components.classes["@mozilla.org/supports-string;1"]
  210.                        .createInstance(Components.interfaces.nsISupportsString);
  211.   wuri.data = submission.uri.spec;
  212.  
  213.   sa.AppendElement(wuri);
  214.   sa.AppendElement(null);
  215.   sa.AppendElement(null);
  216.   sa.AppendElement(submission.postData);
  217.  
  218.   // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
  219.   // preferences, but need nsIBrowserDOMWindow extensions
  220.  
  221.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  222.                          .getService(nsIWindowWatcher);
  223.  
  224.   return wwatch.openWindow(null, nsBrowserContentHandler.chromeURL,
  225.                            "_blank",
  226.                            "chrome,dialog=no,all" +
  227.                              nsBrowserContentHandler.getFeatures(cmdLine),
  228.                            sa);
  229. }
  230.  
  231. var nsBrowserContentHandler = {
  232.   /* helper functions */
  233.  
  234.   mChromeURL : null,
  235.  
  236.   get chromeURL() {
  237.     if (this.mChromeURL) {
  238.       return this.mChromeURL;
  239.     }
  240.  
  241.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  242.                           .getService(nsIPrefBranch);
  243.     this.mChromeURL = prefb.getCharPref("browser.chromeURL");
  244.  
  245.     return this.mChromeURL;
  246.   },
  247.  
  248.   /* nsISupports */
  249.   QueryInterface : function bch_QI(iid) {
  250.     if (!iid.equals(nsISupports) &&
  251.         !iid.equals(nsICommandLineHandler) &&
  252.         !iid.equals(nsIBrowserHandler) &&
  253.         !iid.equals(nsIContentHandler) &&
  254.         !iid.equals(nsICommandLineValidator) &&
  255.         !iid.equals(nsIFactory))
  256.       throw Components.results.NS_ERROR_NO_INTERFACE;
  257.  
  258.     return this;
  259.   },
  260.  
  261.   /* nsICommandLineHandler */
  262.   handle : function bch_handle(cmdLine) {
  263.     if (cmdLine.handleFlag("browser", false)) {
  264.       openWindow(null, this.chromeURL, "_blank",
  265.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  266.                  this.defaultArgs);
  267.       cmdLine.preventDefault = true;
  268.     }
  269.  
  270.     try {
  271.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  272.     }
  273.     catch (e) {
  274.       throw NS_ERROR_ABORT;
  275.     }
  276.  
  277.     if (remoteCommand != null) {
  278.       try {
  279.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  280.         var remoteVerb;
  281.         if (a) {
  282.           remoteVerb = a[1].toLowerCase();
  283.           var remoteParams = [];
  284.           var sepIndex = a[2].lastIndexOf(",");
  285.           if (sepIndex == -1)
  286.             remoteParams[0] = a[2];
  287.           else {
  288.             remoteParams[0] = a[2].substring(0, sepIndex);
  289.             remoteParams[1] = a[2].substring(sepIndex + 1);
  290.           }
  291.         }
  292.  
  293.         switch (remoteVerb) {
  294.         case "openurl":
  295.         case "openfile":
  296.           // openURL(<url>)
  297.           // openURL(<url>,new-window)
  298.           // openURL(<url>,new-tab)
  299.  
  300.           // First param is the URL, second param (if present) is the "target"
  301.           // (tab, window)
  302.           var url = remoteParams[0];
  303.           var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
  304.           if (remoteParams[1]) {
  305.             var targetParam = remoteParams[1].toLowerCase()
  306.                                              .replace(/^\s*|\s*$/g, "");
  307.             if (targetParam == "new-tab")
  308.               target = nsIBrowserDOMWindow.OPEN_NEWTAB;
  309.             else if (targetParam == "new-window")
  310.               target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
  311.             else {
  312.               // The "target" param isn't one of our supported values, so
  313.               // assume it's part of a URL that contains commas.
  314.               url += "," + remoteParams[1];
  315.             }
  316.           }
  317.  
  318.           var uri = resolveURIInternal(cmdLine, url);
  319.           handURIToExistingBrowser(uri, target, cmdLine);
  320.           break;
  321.  
  322.         case "xfedocommand":
  323.           // xfeDoCommand(openBrowser)
  324.           if (remoteParams[0].toLowerCase() != "openbrowser")
  325.             throw NS_ERROR_ABORT;
  326.  
  327.           openWindow(null, this.chromeURL, "_blank",
  328.                      "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  329.                      this.defaultArgs);
  330.           break;
  331.  
  332.         default:
  333.           // Somebody sent us a remote command we don't know how to process:
  334.           // just abort.
  335.           throw "Unknown remote command.";
  336.         }
  337.  
  338.         cmdLine.preventDefault = true;
  339.       }
  340.       catch (e) {
  341.         Components.utils.reportError(e);
  342.         // If we had a -remote flag but failed to process it, throw
  343.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  344.         // back to the handling code.
  345.         throw NS_ERROR_ABORT;
  346.       }
  347.     }
  348.  
  349.     var uriparam;
  350.     try {
  351.       while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
  352.         var uri = resolveURIInternal(cmdLine, uriparam);
  353.         if (!shouldLoadURI(uri))
  354.           continue;
  355.         openWindow(null, this.chromeURL, "_blank",
  356.                    "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  357.                    uri.spec);
  358.         cmdLine.preventDefault = true;
  359.       }
  360.     }
  361.     catch (e) {
  362.       Components.utils.reportError(e);
  363.     }
  364.  
  365.     try {
  366.       while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
  367.         var uri = resolveURIInternal(cmdLine, uriparam);
  368.         handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
  369.         cmdLine.preventDefault = true;
  370.       }
  371.     }
  372.     catch (e) {
  373.       Components.utils.reportError(e);
  374.     }
  375.  
  376.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  377.     if (chromeParam) {
  378.  
  379.       // Handle the old preference dialog URL separately (bug 285416)
  380.       if (chromeParam == "chrome://browser/content/pref/pref.xul") {
  381.         openPreferences();
  382.         cmdLine.preventDefault = true;
  383.       } else try {
  384.         // only load URIs which do not inherit chrome privs
  385.         var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
  386.         var uri = resolveURIInternal(cmdLine, chromeParam);
  387.         var netutil = Components.classes["@mozilla.org/network/util;1"]
  388.                                 .getService(nsINetUtil);
  389.         if (!netutil.URIChainHasFlags(uri, URI_INHERITS_SECURITY_CONTEXT)) {
  390.           openWindow(null, uri.spec, "_blank", features, "");
  391.           cmdLine.preventDefault = true;
  392.         }
  393.       }
  394.       catch (e) {
  395.         Components.utils.reportError(e);
  396.       }
  397.     }
  398.     if (cmdLine.handleFlag("preferences", false)) {
  399.       openPreferences();
  400.       cmdLine.preventDefault = true;
  401.     }
  402.     if (cmdLine.handleFlag("silent", false))
  403.       cmdLine.preventDefault = true;
  404.  
  405.     var searchParam = cmdLine.handleFlagWithParam("search", false);
  406.     if (searchParam) {
  407.       doSearch(searchParam, cmdLine);
  408.       cmdLine.preventDefault = true;
  409.     }
  410.  
  411. //@line 467 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  412.     // Handle "? searchterm" for Windows Vista start menu integration
  413.     for (var i = cmdLine.length - 1; i >= 0; --i) {
  414.       var param = cmdLine.getArgument(i);
  415.       if (param.match(/^\? /)) {
  416.         cmdLine.removeArguments(i, i);
  417.         cmdLine.preventDefault = true;
  418.  
  419.         searchParam = param.substr(2);
  420.         doSearch(searchParam, cmdLine);
  421.       }
  422.     }
  423. //@line 479 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  424.   },
  425.  
  426.   helpInfo : "  -browser            Open a browser window.\n",
  427.  
  428.   /* nsIBrowserHandler */
  429.  
  430.   get defaultArgs() {
  431.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  432.                           .getService(nsIPrefBranch);
  433.     var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
  434.                               .getService(Components.interfaces.nsIURLFormatter);
  435.  
  436.     var overridePage = "";
  437.     var haveUpdateSession = false;
  438.     try {
  439.       switch (needHomepageOverride(prefb)) {
  440.         case OVERRIDE_NEW_PROFILE:
  441.           // New profile
  442.           overridePage = formatter.formatURLPref("startup.homepage_welcome_url");
  443.           break;
  444.         case OVERRIDE_NEW_MSTONE:
  445.           // Existing profile, new build
  446.           copyPrefOverride();
  447.  
  448.           // Check whether we have a session to restore. If we do, we assume
  449.           // that this is an "update" session.
  450.           var ss = Components.classes["@mozilla.org/browser/sessionstartup;1"]
  451.                              .getService(Components.interfaces.nsISessionStartup);
  452.           haveUpdateSession = ss.doRestore();
  453.           overridePage = formatter.formatURLPref("startup.homepage_override_url");
  454.           break;
  455.     }
  456.     } catch (ex) {}
  457.  
  458.     // formatURLPref might return "about:blank" if getting the pref fails
  459.     if (overridePage == "about:blank")
  460.       overridePage = "";
  461.  
  462.     var startPage = "";
  463.     try {
  464.       var choice = prefb.getIntPref("browser.startup.page");
  465.       if (choice == 1)
  466.         startPage = this.startPage;
  467.  
  468.       if (choice == 2)
  469.         startPage = Components.classes["@mozilla.org/browser/global-history;2"]
  470.                               .getService(nsIBrowserHistory).lastPageVisited;
  471.     } catch (e) {
  472.       Components.utils.reportError(e);
  473.     }
  474.  
  475.     if (startPage == "about:blank")
  476.       startPage = "";
  477.  
  478.     // Only show the startPage if we're not restoring an update session.
  479.     if (overridePage && startPage && !haveUpdateSession)
  480.       return overridePage + "|" + startPage;
  481.  
  482.     return overridePage || startPage || "about:blank";
  483.   },
  484.  
  485.   get startPage() {
  486.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  487.                           .getService(nsIPrefBranch);
  488.  
  489.     var uri = prefb.getComplexValue("browser.startup.homepage",
  490.                                     nsIPrefLocalizedString).data;
  491.  
  492.     if (!uri) {
  493.       prefb.clearUserPref("browser.startup.homepage");
  494.       uri = prefb.getComplexValue("browser.startup.homepage",
  495.                                   nsIPrefLocalizedString).data;
  496.     }
  497.                                 
  498.     var count;
  499.     try {
  500.       count = prefb.getIntPref("browser.startup.homepage.count");
  501.     }
  502.     catch (e) {
  503.       return uri;
  504.     }
  505.  
  506.     for (var i = 1; i < count; ++i) {
  507.       try {
  508.         var page = prefb.getComplexValue("browser.startup.homepage." + i,
  509.                                          nsIPrefLocalizedString).data;
  510.         uri += "\n" + page;
  511.       }
  512.       catch (e) {
  513.       }
  514.     }
  515.  
  516.     return uri;
  517.   },
  518.  
  519.   mFeatures : null,
  520.  
  521.   getFeatures : function bch_features(cmdLine) {
  522.     if (this.mFeatures === null) {
  523.       this.mFeatures = "";
  524.  
  525.       try {
  526.         var width = cmdLine.handleFlagWithParam("width", false);
  527.         var height = cmdLine.handleFlagWithParam("height", false);
  528.  
  529.         if (width)
  530.           this.mFeatures += ",width=" + width;
  531.         if (height)
  532.           this.mFeatures += ",height=" + height;
  533.       }
  534.       catch (e) {
  535.       }
  536.     }
  537.  
  538.     return this.mFeatures;
  539.   },
  540.  
  541.   /* nsIContentHandler */
  542.  
  543.   handleContent : function bch_handleContent(contentType, context, request) {
  544.     try {
  545.       var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
  546.                                  .getService(nsIWebNavigationInfo);
  547.       if (!webNavInfo.isTypeSupported(contentType, null)) {
  548.         throw NS_ERROR_WONT_HANDLE_CONTENT;
  549.       }
  550.     } catch (e) {
  551.       throw NS_ERROR_WONT_HANDLE_CONTENT;
  552.     }
  553.  
  554.     request.QueryInterface(nsIChannel);
  555.     handURIToExistingBrowser(request.URI,
  556.       nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, null);
  557.     request.cancel(NS_BINDING_ABORTED);
  558.   },
  559.  
  560.   /* nsICommandLineValidator */
  561.   validate : function bch_validate(cmdLine) {
  562.     // Other handlers may use osint so only handle the osint flag if the url
  563.     // flag is also present and the command line is valid.
  564.     var osintFlagIdx = cmdLine.findFlag("osint", false);
  565.     var urlFlagIdx = cmdLine.findFlag("url", false);
  566.     if (urlFlagIdx > -1 && (osintFlagIdx > -1 ||
  567.         cmdLine.state == nsICommandLine.STATE_REMOTE_EXPLICIT)) {
  568.       var urlParam = cmdLine.getArgument(urlFlagIdx + 1);
  569.       if (cmdLine.length != urlFlagIdx + 2 || /firefoxurl:/.test(urlParam))
  570.         throw NS_ERROR_ABORT;
  571.       cmdLine.handleFlag("osint", false)
  572.     }
  573.   },
  574.  
  575.   /* nsIFactory */
  576.   createInstance: function bch_CI(outer, iid) {
  577.     if (outer != null)
  578.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  579.  
  580.     return this.QueryInterface(iid);
  581.   },
  582.     
  583.   lockFactory : function bch_lock(lock) {
  584.     /* no-op */
  585.   }
  586. };
  587.  
  588. const bch_contractID = "@mozilla.org/browser/clh;1";
  589. const bch_CID = Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}");
  590. const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type=";
  591.  
  592. function handURIToExistingBrowser(uri, location, cmdLine)
  593. {
  594.   if (!shouldLoadURI(uri))
  595.     return;
  596.  
  597.   var navWin = getMostRecentBrowserWindow();
  598.   if (!navWin) {
  599.     // if we couldn't load it in an existing window, open a new one
  600.     openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  601.                "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  602.                uri.spec);
  603.     return;
  604.   }
  605.  
  606.   var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
  607.                      .getInterface(nsIWebNavigation);
  608.   var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
  609.   var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
  610.                         .getInterface(nsIDOMWindow);
  611.   var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
  612.   bwin.openURI(uri, null, location,
  613.                nsIBrowserDOMWindow.OPEN_EXTERNAL);
  614. }
  615.  
  616.  
  617. var nsDefaultCommandLineHandler = {
  618.   /* nsISupports */
  619.   QueryInterface : function dch_QI(iid) {
  620.     if (!iid.equals(nsISupports) &&
  621.         !iid.equals(nsICommandLineHandler) &&
  622.         !iid.equals(nsIFactory))
  623.       throw Components.results.NS_ERROR_NO_INTERFACE;
  624.  
  625.     return this;
  626.   },
  627.  
  628.   // List of uri's that were passed via the command line without the app
  629.   // running and have already been handled. This is compared against uri's
  630.   // opened using DDE on Win32 so we only open one of the requests.
  631.   _handledURIs: [ ],
  632. //@line 688 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  633.   _haveProfile: false,
  634. //@line 690 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  635.  
  636.   /* nsICommandLineHandler */
  637.   handle : function dch_handle(cmdLine) {
  638.     var urilist = [];
  639.  
  640. //@line 696 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  641.     // If we don't have a profile selected yet (e.g. the Profile Manager is
  642.     // displayed) we will crash if we open an url and then select a profile. To
  643.     // prevent this handle all url command line flags and set the command line's
  644.     // preventDefault to true to prevent the display of the ui. The initial
  645.     // command line will be retained when nsAppRunner calls LaunchChild though
  646.     // urls launched after the initial launch will be lost.
  647.     if (!this._haveProfile) {
  648.       try {
  649.         // This will throw when a profile has not been selected.
  650.         var fl = Components.classes["@mozilla.org/file/directory_service;1"]
  651.                            .getService(Components.interfaces.nsIProperties);
  652.         var dir = fl.get("ProfD", Components.interfaces.nsILocalFile);
  653.         this._haveProfile = true;
  654.       }
  655.       catch (e) {
  656.         while ((ar = cmdLine.handleFlagWithParam("url", false))) { }
  657.         cmdLine.preventDefault = true;
  658.       }
  659.     }
  660. //@line 716 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  661.  
  662.     try {
  663.       var ar;
  664.       while ((ar = cmdLine.handleFlagWithParam("url", false))) {
  665.         var found = false;
  666.         var uri = resolveURIInternal(cmdLine, ar);
  667.         // count will never be greater than zero except on Win32.
  668.         var count = this._handledURIs.length;
  669.         for (var i = 0; i < count; ++i) {
  670.           if (this._handledURIs[i].spec == uri.spec) {
  671.             this._handledURIs.splice(i, 1);
  672.             found = true;
  673.             cmdLine.preventDefault = true;
  674.             break;
  675.           }
  676.         }
  677.         if (!found) {
  678.           urilist.push(uri);
  679.           // The requestpending command line flag is only used on Win32.
  680.           if (cmdLine.handleFlag("requestpending", false) &&
  681.               cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
  682.             this._handledURIs.push(uri)
  683.         }
  684.       }
  685.     }
  686.     catch (e) {
  687.       Components.utils.reportError(e);
  688.     }
  689.  
  690.     count = cmdLine.length;
  691.  
  692.     for (i = 0; i < count; ++i) {
  693.       var curarg = cmdLine.getArgument(i);
  694.       if (curarg.match(/^-/)) {
  695.         Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
  696.         // To emulate the pre-nsICommandLine behavior, we ignore
  697.         // the argument after an unrecognized flag.
  698.         ++i;
  699.       } else {
  700.         try {
  701.           urilist.push(resolveURIInternal(cmdLine, curarg));
  702.         }
  703.         catch (e) {
  704.           Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
  705.         }
  706.       }
  707.     }
  708.  
  709.     if (urilist.length) {
  710.       if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
  711.           urilist.length == 1) {
  712.         // Try to find an existing window and load our URI into the
  713.         // current tab, new tab, or new window as prefs determine.
  714.         try {
  715.           handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
  716.           return;
  717.         }
  718.         catch (e) {
  719.         }
  720.       }
  721.  
  722.       var speclist = [];
  723.       for (uri in urilist) {
  724.         if (shouldLoadURI(urilist[uri]))
  725.           speclist.push(urilist[uri].spec);
  726.       }
  727.  
  728.       if (speclist.length) {
  729.         openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  730.                    "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  731.                    speclist.join("|"));
  732.       }
  733.  
  734.     }
  735.     else if (!cmdLine.preventDefault) {
  736.       openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  737.                  "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  738.                  nsBrowserContentHandler.defaultArgs);
  739.     }
  740.   },
  741.  
  742.   // XXX localize me... how?
  743.   helpInfo : "Usage: firefox [-flags] [<url>]\n",
  744.  
  745.   /* nsIFactory */
  746.   createInstance: function dch_CI(outer, iid) {
  747.     if (outer != null)
  748.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  749.  
  750.     return this.QueryInterface(iid);
  751.   },
  752.     
  753.   lockFactory : function dch_lock(lock) {
  754.     /* no-op */
  755.   }
  756. };
  757.  
  758. const dch_contractID = "@mozilla.org/browser/final-clh;1";
  759. const dch_CID = Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}");
  760.  
  761. var Module = {
  762.   /* nsISupports */
  763.   QueryInterface: function mod_QI(iid) {
  764.     if (iid.equals(Components.interfaces.nsIModule) ||
  765.         iid.equals(Components.interfaces.nsISupports))
  766.       return this;
  767.  
  768.     throw Components.results.NS_ERROR_NO_INTERFACE;
  769.   },
  770.  
  771.   /* nsIModule */
  772.   getClassObject: function mod_getco(compMgr, cid, iid) {
  773.     if (cid.equals(bch_CID))
  774.       return nsBrowserContentHandler.QueryInterface(iid);
  775.  
  776.     if (cid.equals(dch_CID))
  777.       return nsDefaultCommandLineHandler.QueryInterface(iid);
  778.  
  779.     throw Components.results.NS_ERROR_NO_INTERFACE;
  780.   },
  781.     
  782.   registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
  783.     // Don't register these if Firefox is launching a XULRunner application
  784.     const FIREFOX_UID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
  785.     var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  786.                             .getService(Components.interfaces.nsIXULAppInfo);
  787.     if (appInfo.ID != FIREFOX_UID)
  788.       return;
  789.  
  790.     var compReg =
  791.       compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  792.  
  793.     compReg.registerFactoryLocation( bch_CID,
  794.                                      "nsBrowserContentHandler",
  795.                                      bch_contractID,
  796.                                      fileSpec,
  797.                                      location,
  798.                                      type );
  799.     compReg.registerFactoryLocation( dch_CID,
  800.                                      "nsDefaultCommandLineHandler",
  801.                                      dch_contractID,
  802.                                      fileSpec,
  803.                                      location,
  804.                                      type );
  805.  
  806.     function registerType(contentType) {
  807.       compReg.registerFactoryLocation( bch_CID,
  808.                                        "Browser Cmdline Handler",
  809.                                        CONTRACTID_PREFIX + contentType,
  810.                                        fileSpec,
  811.                                        location,
  812.                                        type );
  813.     }
  814.  
  815.     registerType("text/html");
  816.     registerType("application/vnd.mozilla.xul+xml");
  817. //@line 873 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  818.     registerType("image/svg+xml");
  819. //@line 875 "e:\builds\tinderbox\Fx-Rel\WINNT_5.2_Depend\mozilla\browser\components\nsBrowserContentHandler.js"
  820.     registerType("text/rdf");
  821.     registerType("text/xml");
  822.     registerType("application/xhtml+xml");
  823.     registerType("text/css");
  824.     registerType("text/plain");
  825.     registerType("image/gif");
  826.     registerType("image/jpeg");
  827.     registerType("image/jpg");
  828.     registerType("image/png");
  829.     registerType("image/bmp");
  830.     registerType("image/x-icon");
  831.     registerType("image/vnd.microsoft.icon");
  832.     registerType("image/x-xbitmap");
  833.     registerType("application/http-index-format");
  834.  
  835.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  836.                            .getService(nsICategoryManager);
  837.  
  838.     catMan.addCategoryEntry("command-line-handler",
  839.                             "m-browser",
  840.                             bch_contractID, true, true);
  841.     catMan.addCategoryEntry("command-line-handler",
  842.                             "x-default",
  843.                             dch_contractID, true, true);
  844.     catMan.addCategoryEntry("command-line-validator",
  845.                             "b-browser",
  846.                             bch_contractID, true, true);
  847.   },
  848.     
  849.   unregisterSelf : function mod_unregself(compMgr, location, type) {
  850.     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
  851.     compReg.unregisterFactoryLocation(bch_CID, location);
  852.     compReg.unregisterFactoryLocation(dch_CID, location);
  853.  
  854.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  855.                            .getService(nsICategoryManager);
  856.  
  857.     catMan.deleteCategoryEntry("command-line-handler",
  858.                                "m-browser", true);
  859.     catMan.deleteCategoryEntry("command-line-handler",
  860.                                "x-default", true);
  861.     catMan.deleteCategoryEntry("command-line-validator",
  862.                                "b-browser", true);
  863.   },
  864.  
  865.   canUnload: function(compMgr) {
  866.     return true;
  867.   }
  868. };
  869.  
  870. // NSGetModule: Return the nsIModule object.
  871. function NSGetModule(compMgr, fileSpec) {
  872.   return Module;
  873. }
  874.